﻿ using Core.Comamad;
using Core.ReadAndWrite;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Core
{
    public class QuanLiDSHocPhan : IQuanLi
    {
        public List<BaseData> ListHocPhan;
        #region Singleton
        private static QuanLiDSHocPhan _Instance;
        private static object locks = new object();

        public static QuanLiDSHocPhan Instance
        {
            get
            {
                if (_Instance == null)
                {
                    lock (locks)
                    {
                        if (_Instance == null)
                            _Instance = new QuanLiDSHocPhan();
                    }
                }
                return _Instance;
            }
        }
        #endregion
        #region Command
        List<ICommand> _commands = new List<ICommand>();
        private int Current
        {
            get
            {
                return _commands.Count;

            }
            set
            {
                Current = value;
            }
        }
        public void Undo()
        {
            if (Current > 0)
            {
                var count = (int)Current - 1;
                ICommand cms = _commands[count];
                cms.UnExecute();
            }
        }
        #endregion
        public QuanLiDSHocPhan()
        {

            ListHocPhan = DataSource.Instance.ReadHocPhan("Dshp.txt");

        }

        public void Add(BaseData ob)
        {
            ListHocPhan.Add(ob);
        }

        public void Delete(BaseData ob)
        {
            ListHocPhan.Remove(ob);
        }

        public void Update(BaseData ob)
        {
            HocPhan hp = ob as HocPhan;
            HocPhan find = ListHocPhan.Find(c => (c as HocPhan).MaHocPhan == hp.MaHocPhan) as HocPhan;
            HocPhan t = new HocPhan
            {
                MaHocPhan = find.MaHocPhan,
                TenHocPhan = find.TenHocPhan,
                SoTC = find.SoTC,
                LoaiHP = find.LoaiHP
            };
            find.MaHocPhan = hp.MaHocPhan;
            find.TenHocPhan = hp.TenHocPhan;
            find.SoTC = hp.SoTC;
            find.LoaiHP = hp.LoaiHP;
            ICommand tem = new ObCommand(LoaiXuLi.Update, t);
            _commands.Add(tem);
        }


        public void getHocPhan()
        {

            ListHocPhan = DataSource.Instance.ReadHocPhan("Dshp.txt");
        }



        public List<BaseData> FindAll(Predicate<BaseData> Predicate)
        {
            throw new NotImplementedException();
        }


        public BaseData Find(Func<BaseData, bool> predicate)
        {
            return ListHocPhan.FirstOrDefault(predicate);
        }
        public override string ToString()
        {
            StringBuilder tem = new StringBuilder();
            foreach (var item in this.ListHocPhan)
            {
                tem.AppendLine(item.ToString());
            }
            return tem.ToString();
        }
    }
}
